<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="名称" prop="name"
                :rules="[
                  {required: true, message:'名称不能为空', trigger:'blur'}
                 ]">
			        <el-input v-model="inputForm.name" placeholder="请填写名称"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="排序" prop="sort"
                :rules="[
                 ]">
			        <el-input v-model="inputForm.sort" placeholder="请填写排序"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="20" :offset="2" v-if="modelType === 'voice'">
          <el-tag size="small" v-for="(word,index) in keyWords" :key="word" closable @close="keyWordClose(index)">
            {{word}}
          </el-tag>
        </el-col>
        <el-col :span="12" v-if="modelType === 'voice'">
            <el-form-item label="关键词" prop="keyWord"
                :rules="[
                 ]">
              <el-input v-model="keyWord" placeholder="关键词" ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12" v-if="modelType === 'voice'">
          <el-button round @click="addKeyWord">添加</el-button>
        </el-col>

        <el-col :span="22" v-if="modelType === 'face'">
          <el-form-item label="年龄层" prop="age"
            :rules="[
             ]">
            <el-checkbox-group
                v-model="ageList">
               <el-checkbox v-for="type in $dictUtils.getDictList('age')" :label="type.value" :key="type.id">{{type.label}}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-col :span="22" v-if="modelType === 'face'">
          <el-form-item label="性别" prop="gender"
            :rules="[
             ]">
            <el-checkbox-group
                v-model="genderList">
               <el-checkbox v-for="type in $dictUtils.getDictList('gender')" :label="type.value" :key="type.id">{{type.label}}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-col :span="22" v-if="modelType === 'face'">
          <el-form-item label="佩戴眼镜" prop="glasses"
            :rules="[
             ]">
            <el-checkbox-group
                v-model="glassesList">
               <el-checkbox v-for="type in $dictUtils.getDictList('glasses')" :label="type.value" :key="type.id">{{type.label}}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-col :span="22" :offset="1" v-if="modelType === 'face'">
          <div style="color: #FAAD14;">备注：只有同时满足以上选中的条件，才被推荐（如海澜之家，需要年龄层：全部勾选，性别：男，佩戴眼镜：全部勾选）</div>
        </el-col>
        <el-col :span="22" :offset="1" v-if="modelType === 'voice'">
          <div style="color: #FAAD14;">备注：语音识别到任一关键词，将推荐展示列表商户</div>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  export default {
    props: {
      modelType: {
        type: String,
        default: () => { return 'face' }
      }
    },
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        keyWords: [],
        keyWord: '',
        ageList: [],
        genderList: [],
        glassesList: [],
        inputForm: {
          id: '',
          name: '',
          tag: '',
          sort: '',
          parent: {
            id: ''
          }
        }
      }
    },
    methods: {
      init (method, obj) {
        console.log(this.$dictUtils.getDictList('gender'))
        this.method = method
        this.inputForm.id = obj.id
        if (method === 'add') {
          this.title = '新建标签'
        } else if (method === 'addChild') {
          this.title = '添加下级标签'
        } else if (method === 'edit') {
          this.title = '修改标签'
        } else if (method === 'view') {
          this.title = '查看标签'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.keyWords = []
          this.keyWord = ''
          this.ageList = []
          this.genderList = []
          this.glassesList = []
          this.inputForm.parent.id = obj.parent.id
          this.inputForm.parent.name = obj.parent.name
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/smis/label/jfLabelGroup/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.jfLabelGroup)
              if(this.inputForm.tag && this.inputForm.tag.length>4) {
                var tag = JSON.parse(this.inputForm.tag)
                this.ageList = tag.age || []
                this.genderList = tag.gender || []
                this.glassesList = tag.glasses || []
                this.keyWords = tag.words || []
              }
              this.loading = false
            })
          }
        })
      },
      addKeyWord() {
        if(this.keyWord === '') {
          this.$message.warning('请填写关键词，长度限制2～6字')
          return
        }
        if(this.keyWord.length<2) {
          this.$message.warning('关键词至少2个字')
          return
        }
        if(this.keyWord.length>6) {
          this.$message.warning('关键词最多6个字')
          return
        }
        for(var i=0; i<this.keyWords.length; i++) {
          if(this.keyWord === this.keyWords[i]) {
            this.$message.warning('关键词已在上列列表中存在')
            return
          }
        }
        this.keyWords.push(this.keyWord)
        this.keyWord = ''
      },
      keyWordClose(index) {
        this.keyWords.splice(index, 1)
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            var tag = {
              "age": this.ageList,
              "gender": this.genderList,
              "glasses": this.glassesList,
              "words": this.keyWords,
            }
            this.inputForm.tag = JSON.stringify(tag)
            this.inputForm.modelType = this.modelType
            this.loading = true
            this.$http({
              url: `/smis/label/jfLabelGroup/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshTree')
              }
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>
